﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using aspZone.Commerce.DataAccess.Repositories;
using NHibernate;
using aspZone.Commerce.DataAccess;
using NHibernate.Tool.hbm2ddl;
using aspZone.Commerce.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace aspZone.Commerce.Tests
{
    [TestClass]
    public class TestDataLoader
    {
        private ISession session;

        public TestDataLoader()
        {
            this.session = DataAccessConfiguration.GetSession();
        }

        [TestMethod]
        public void LoadData()
        {
            this.ExportSchema();

            var quarters = AddToCategoryMenu("Quarters", "This is the quarters category", null);
            var quarterBankRolls = AddToCategoryMenu("Bank Rolls", null, quarters);
            var quarterMintRolls = AddToCategoryMenu("Mint Rolls", null, quarters);
            var quarterMintBags = AddToCategoryMenu("Mint Bags", null, quarters);
            var quarterProofSets = AddToCategoryMenu("Proof Sets", null, quarters);

            var dollars = AddToCategoryMenu("Dollars", "This is the dollars category", null);
            var ikeDollars = AddToCategoryMenu("Eisenhower", null, dollars);
            var sacDollars = AddToCategoryMenu("Sacagawea", null, dollars);
            var presDollars = AddToCategoryMenu("Presidential", null, dollars);
            var morganDollars = AddToCategoryMenu("Morgan", null, dollars);
            var oldMorganDollars = AddToCategoryMenu("Pre-1921", null, morganDollars);
            var newMorganDollars = AddToCategoryMenu("1921", null, morganDollars);

            var goldSolver = AddToCategoryMenu("Gold & Silver", "This is the gold and silver category", null);

            var clearance = AddToTopMenu("Clearance Items", null);
            var newArrivals = AddToTopMenu("New Arrivals", null);


            AddProduct("2008 Hawaii D State Quarter H/T BU Roll", "The Hawaii quarter-dollar is the 50th and final coin released in the 50 State Quarters® Program. This bag is filled with 100 Hawaii quarters struck on the main production floor of the Denver facility and bears a “D” mint mark. Hawaii, nicknamed “The Aloha State,” was admitted to the union on August 21, 1959. The reverse of the Hawaii quarter features Hawaiian monarch King Kamehameha I stretching out his hand toward the eight major Hawaiian Islands. The coin’s design includes the state motto “UA MAU KE EA O KA ‘ĀINA I KA PONO” (The life of the land is perpetuated in righteousness), “Hawaii” and “1959.”", 9.99m, quarterBankRolls, newArrivals);

            AddProduct("1900 P Morgan #20008", "1900 Philidelphia Mint Morgan Silver Dollar #20008", 9.99m, oldMorganDollars);
            AddProduct("1886 P Morgan #30005", "1886 Philidelphia Mint Morgan Silver Dollar #30005", 9.99m, oldMorganDollars);
            AddProduct("1886 S Morgan #30006", "1886 Philidelphia Mint Morgan Silver Dollar #30005", 9.99m, oldMorganDollars);
            AddProduct("1886 CC Morgan #30007", "1886 Philidelphia Mint Morgan Silver Dollar #30005", 9.99m, oldMorganDollars);
            AddProduct("1886 O Morgan #30008", "1886 Philidelphia Mint Morgan Silver Dollar #30005", 9.99m, oldMorganDollars);
            AddProduct("1887 O Morgan #30009", "1886 Philidelphia Mint Morgan Silver Dollar #30005", 9.99m, oldMorganDollars);

            AddProduct("2007 P & D Set Sacagawea Dollar Uncirculated - BU", "This auction is for two 2008 Sacagawea Golden Dollars.  One from the Philadelphia (P) Mint  & one from the Denver (D) Mint.  Both in brilliant uncirculated mint condition.", 9.99m, sacDollars);
            AddProduct("2007 Sacagawea D US Mint Roll 6M4", "2007 Sacagawea D  Sealed $25  Roll 6M4 Sealed in US Mint Box!", 9.99m, sacDollars);
            AddProduct("2007 D Thomas Jefferson Mint Roll TJ 4 Sealed Box", "2007 D Thomas Jefferson Mint Roll TJ 4 Sealed Box", 9.99m, presDollars);

            this.session.Flush();
        }

        private void ExportSchema()
        {
            new SchemaExport(DataAccessConfiguration.NHibernateConfiguration).Create(true, true);
        }

        private Product AddProduct(string name, string description, decimal price, params Category[] categories)
        {
            var p = new Product { Name = name, Description = description, Price = price, HandlingCharge = 0.99m, QuantityAvailable = 100 };
            p.Categories.AddAll(categories);
            this.session.Save(p);
            return p;
        }

        private Category AddToCategoryMenu(string name, string description, Category parent)
        {
            var c = new Category { Name = name, Description = description, ParentCategory = parent, DisplayInCategoryMenu = true, DisplayInTopMenu = false };
            this.session.Save(c);
            return c;
        }

        private Category AddToTopMenu(string name, string description)
        {
            var c = new Category { Name = name, Description = description, DisplayInCategoryMenu = false, DisplayInTopMenu = true };
            this.session.Save(c);
            return c;
        }
    }
}
